package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.service.GraphInfoService;
import com.atguigu.stgy.model.dto.ApartmentDTO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.model.entity.GraphInfo;
import com.atguigu.stgy.admin.mapper.GraphInfoMapper;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

@Service
public class GraphInfoServiceImpl extends ServiceImpl<GraphInfoMapper, GraphInfo> implements GraphInfoService {


    @Override
    public void batchSaveGraphInfo(ApartmentDTO apartmentDTO, Long apartmentInfoId) {
        if(CollectionUtils.isEmpty(apartmentDTO.getGraphVoList())){
            return;
        }
//        Assert.isTrue(!CollectionUtils.isEmpty(apartmentDTO.getGraphVoList()) ,);
        apartmentDTO.getGraphVoList().forEach(graphInfo -> {
            //设置图片所属的公寓id
            graphInfo.setItemId(apartmentInfoId);
            //图片类型  1公寓  2room
            graphInfo.setItemType((byte) 1);
        });
        this.saveBatch(apartmentDTO.getGraphVoList());
    }

    @Override
    public void updateApartmentGraphInfos(ApartmentDTO apartmentDTO) {
        //删除所有图片
        this.remove(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId , apartmentDTO.getId()));
        //图片查询回显时查询了自己的id，上面虽然逻辑删除了 但是数据库表中id依然存在
        //设置本次要 保存的图片id为null，新增时会自动生成新的id
        apartmentDTO.getGraphVoList().forEach(graphInfo -> graphInfo.setId(null));
        //保存本次的图片列表
        this.batchSaveGraphInfo(apartmentDTO , apartmentDTO.getId());
    }
}
